/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

import pojo.SanPham;
import util.NewHibernateUtil;

/**
 *
 * @author 0
 */
public class SanPhamDAO extends BaseDAO{
    public static void ThemSanPham(SanPham sanPham) throws Exception
    {
        Them(sanPham);
    }
     
    public static void XoaSanPham(Object[] sanPhamlist) throws Exception
    {
         Xoa(sanPhamlist);
    }
    
     public static void SuaSanPham(SanPham sanPham) throws Exception{
        Sua(sanPham);
    }
      public static List<SanPham> TimKiemSanPham(Integer maLoaiSanPham,String tenLoaiSanPham,Integer maSanPham,String tenSanPham,Double donGiaTu, Double donGiaDen,boolean forDialogSearch) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String string="from SanPham sp where (sp.isDelete=false or sp.isDelete=null)";
//        if (nguoiDung.getLoaiNguoiDung().getId() != LoaiNguoiDungEnum.Admin.getCode() || forDialogSearch) {
//            string += String.format(" and spch.cuaHang.id=%d", nguoiDung.getCuaHang().getId());
//        }
        if(maLoaiSanPham!=null){
            string+=String.format(" and sp.loaiSanPham =%d", maLoaiSanPham);
        }
        if(!tenLoaiSanPham.isEmpty()){
            string+=" and lower(sp.loaiSanPham.tenLoaiSanPham) like :tenLoaiSanPham";
        }
        if(maSanPham!=null){
            string+=String.format(" and sp.id =%d", maSanPham);
        }
        if(!tenSanPham.isEmpty()){
            string+=" and lower(sp.tenSanPham) like :tenSanPham";
        }
        if(donGiaTu !=null && donGiaDen !=null){
            string +=String.format("and sp.donGia >=%f and sp.donGia <=%f",donGiaTu,donGiaDen);
        }
        Query query=session.createQuery(string);
        if(!tenLoaiSanPham.isEmpty()){
            query.setString("tenLoaiSanPham", "%"+tenLoaiSanPham.toLowerCase()+"%");
        }
        if(!tenSanPham.isEmpty()){
            query.setString("tenSanPham", "%"+tenSanPham.toLowerCase()+"%");
        }
        List<SanPham>sanPhams=query.list();
        session.close();
        return sanPhams;
    }

}
